package com.roadmap.ajax.handler;

import org.apache.log4j.Logger;

import com.roadmap.ajax.proxy.BaseProxy;
import com.roadmap.biz.common.IManager;
import com.roadmap.biz.common.ManagerFactory;
import com.roadmap.common.BaseEntity;
import com.roadmap.common.RdcConst;
import com.roadmap.common.RdcLogger;
import com.roadmap.entities.Aspirant;
import com.roadmap.entities.Career;
import com.roadmap.entities.Contact;
import com.roadmap.entities.Student;
import com.roadmap.struts.action.ActionConst;

public class ProfileHandler extends BaseProxy {
	private final static Logger logger = RdcLogger
			.getLogger(ProfileHandler.class);

	public boolean saveBasicInfo(Aspirant aspirant) {
		if (aspirant == null || aspirant.getId() == null) {
			logger.warn("entity or its owner id is NULL.");
			return false;
		}

		try {
			String uid = getUIDInSession();
			IManager mgr = ManagerFactory.getInstance(RdcConst.USER, uid);
			Aspirant user = (Aspirant) mgr.get(uid);
			// some properties should not be changed here
			aspirant.setMail(user.getMail());
			aspirant.setLoginIP(user.getLoginIP());
			aspirant.setLoginPassword(user.getLoginPassword());
			aspirant.setCreatedTime(user.getCreatedTime());
			aspirant.setImagePath(user.getImagePath());
			update(aspirant);

			// update the name stored in the present session
			getSession().setAttribute(ActionConst.Aspirant_Name_In_Session,
					aspirant.getRealName());

			return true;
		}
		catch (Exception e) {
			logger.error("failed to update the basic information for "
					+ aspirant.getId(), e);
		}
		return false;
	}

	public String saveContact(Contact cont) {
		if (cont == null || cont.getAspirantId() == null) {
			logger.warn("entity or its owner id is NULL.");
			return null;
		}
		try {
			if (isEntityNew(cont)) {
				create(cont);
			}
			else {
				update(cont);
			}
			return cont.getId();
		}
		catch (Exception e) {
			logger.error("failed to update the contact for "
					+ cont.getAspirantId(), e);
		}
		return null;
	}

	public String saveCareer(Career carr) {
		if (carr == null || carr.getAspirantId() == null) {
			logger.warn("entity or its owner id is NULL.");
			return null;
		}
		try {
			if (isEntityNew(carr))
				create(carr);
			else {
				update(carr);
			}

			return carr.getId();
		}
		catch (Exception e) {
			logger.error("failed to update the career for "
					+ carr.getAspirantId(), e);
		}
		return null;
	}

	public String saveStudent(Student stud) {
		if (stud == null || stud.getAspirantId() == null) {
			logger.warn("entity or its owner id is NULL.");
			return null;
		}
		try {
			if (isEntityNew(stud))
				create(stud);
			else {
				update(stud);
			}

			return stud.getId();
		}
		catch (Exception e) {
			logger.error("failed to update the education for "
					+ stud.getAspirantId(), e);
		}
		return null;
	}

	private boolean isEntityNew(BaseEntity entity) {
		return entity.getId() == null || entity.getId().trim().equals("");
	}
}
